package uf.tax.laws.spider.sd;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import uf.tax.laws.spider.HttpDAO;
import uf.tax.laws.spider.Utils;


/**
 * Main Class
 * 
 * @author sunny
 *
 */
public class App {
	private static final String UTF_8 = "utf-8";
	private HttpDAO http = new HttpDAO();
	private String base_dir;

	private boolean login(String name, String password) {
		http.setEncoding(UTF_8);
		Login login = new Login(http);
		try {
			return login.login(name, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private Map<String, String> getCategories() {
		GetCatList categories = new GetCatList(http);
		return categories.getLists();
	}

	private int fileCount = 0;

	private static String http_header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
			+ "<html xmlns=\"http://www.w3.org/1999/xhtml\" >"
			+ "<head id=\"Head1\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=7\" /><title>"
			+ "       ##title##" + "</title><base href=\"http://www.taxad.cn\" />"
			+ "<meta http-equiv=\"Content-Language\" content=\"zh-CN\" /><link href=\"/css/css.css\" rel=\"stylesheet\" type=\"text/css\" /><link rel=\"shortcut icon\" href=\"../favicon.ico\" />"
			+ "<script type=\"text/javascript\" src=\"/js/ks.js\" /></script>"
			+ "<script type=\"text/javascript\" src=\"/js/ad.js\" /></script>" + "<script src=\"/js/show.js\"></script>"
			+ "</head>" + "<body>";

	private static final Pattern pattern = Pattern.compile(
			"<li style=\"font-weight:bold; line-height:30px; padding-left:10px; font-size:15px; padding:5px 10px 5px 10px; text-align:center; border-bottom:1px solid #dedede\">(.*?)<ul style=\"width:250px; padding-left:175px\">",
			Pattern.DOTALL);

	private String handleContent(String content, String title) {
		StringBuilder sb = new StringBuilder();
		sb.append(http_header.replace("##title##", title));
		Matcher matcher = pattern.matcher(content);
		if (matcher.find()) {
			String innerContent = matcher.group(1);
			innerContent = innerContent.replaceFirst("<br><b>更新日期：.*?</a>", "");
			sb.append(
					"<li style=\"font-weight:bold; line-height:30px; padding-left:10px; font-size:15px; padding:5px 10px 5px 10px; text-align:center; border-bottom:1px solid #dedede\">"
							+ innerContent + "</div>");
		}
		sb.append("</body></html>");
		return sb.toString();
	}

	public void start(String name, String passwd) {
		if (!login(name, passwd)) {
			System.out.println("process error");
			return;
		}
		// 获取类型
		Map<String, String> cats = getCategories();
		Iterator<String> catKysIterator = cats.keySet().iterator();
		while (catKysIterator.hasNext()) {
			String catString = catKysIterator.next();
			if (catString.equals("不限税种"))
				continue;
			String catId = cats.get(catString);
			System.out.println("process category: " + catString);
			File dirFile = new File(base_dir + "/" + catString);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			// 进入列表页
			GetLawList laws = new GetLawList(http);
			int cnt = laws.getItemCountByCatId(catId);
			int pageCnt = (cnt + 14) / 15; // 获取页面数量
			System.out.println("pages: " + pageCnt);
			for (int i = 1; i <= pageCnt; i++) { // 进入每页进行处理
				System.out.println("process page: " + i);
				Map<String, String> items = laws.getItems(catId, i);
				Iterator<String> ikIterator = items.keySet().iterator();
				while (ikIterator.hasNext()) {
					String fileName = ikIterator.next();
					String url = items.get(fileName);
					GetLawItem item = new GetLawItem(http);
					String content = item.get(url); // 获取每页内容
					try {
						content = handleContent(content, fileName); // 内容清洗为需要的数据
						fileName = fileName.replaceAll("《", "").replaceAll("》", "").replaceAll("<", "").replaceAll(">",
								""); // 过滤文件名中的特殊字符
						Utils.strToFile(content, dirFile.getAbsolutePath() + "/" + fileName + ".html", UTF_8); // 保存内容
						fileCount++;
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	static Properties config;

	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println(
					"Usage: App properties-file\nProperties format:\nstore_dir=/law/to/store/directory\nname=login name\npasswd=login password");
			return;
		}

		config = Utils.getConfig(args[0]);

		System.out.println("start...");
		App app = new App();
		app.base_dir = config.getProperty("store_dir", null);
		System.out.println("base dir: " + app.base_dir);
		if (app.base_dir == null) {
			System.out.println("store dir is invalid");
			System.exit(1);
		}
		String name = config.getProperty("name");
		String passwd = config.getProperty("passwd");
		app.start(name, passwd); // "whxy001", "123456"
		System.out.println("processed! " + app.fileCount);
	}
}
